
function Loaded(cb) {
    document.addEventListener('DOMContentLoaded', function(){
        cb();
    });
}

function post(url, data, cb) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.onload = function(e){
        if(this.status == 200 || this.status == 304){
            cb && cb(JSON.parse(this.responseText));
        }
    };
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    xhr.send(data);
}

function buildData(vue, field) {
    var data = [];
    for(var i in field){
        if(vue[field[i]]){
            data.push(field[i] + '=' + vue[field[i]]);
        }
    }
    return encodeURI(data.join('&'));
}

function Ajax(url, data, success, onprogress, timeout, error, responseType, type) {
    //构造表单数据
    type = type ? type.toUpperCase() : 'POST';
    responseType = responseType ? responseType : 'json';
    //创建xhr对象
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
    xhr.timeout = 10000;
    //设置响应返回的数据格式
    xhr.responseType = responseType;
    //创建一个 post 请求，采用异步
    if('GET' === type){
        var dataMap = [];
        for(var i in data){
            dataMap.push(i + '=' + data[i]);
        }
        url = url + '?' + dataMap.join('&');
    }
    xhr.open(type, url, true);
    if('POST' === type){
        if(data instanceof FormData){
            formData = data;
        }else{
            var formData = new FormData();
            for(var i in data){
                formData.append(i, data[i]);
            }
        }
    }
    //注册相关事件回调处理函数
    xhr.onload = function(e) {
        if(this.status === 200 || this.status === 304){
            success && success(this.response);
        }
    };
    xhr.ontimeout = function(e) {
        timeout && timeout(e);
    };
    xhr.onerror = function(e) {
        error && error(e);
    };
    xhr.upload.onprogress = function(e) {
        onprogress && onprogress(e);
    };

    //发送数据
    xhr.send('POST' === type && formData);
}

function jump(url){
    if(url){
        window.location.assign(url)
    }else{
        history.go(-1);
    }
}